/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.staf.SnmpTopologyExplorer;

import java.util.Vector;

import java.io.IOException;
import org.snmp4j.*;
import org.snmp4j.event.*;
import org.snmp4j.smi.*;
import org.snmp4j.transport.*;
import org.snmp4j.mp.SnmpConstants;

/**
 *
 * @author aezhov
 */
public class SnmpAgent extends GenericAgent implements ResponseListener {

    Address targetAddress = null;
    Snmp snmp = null;
    CommunityTarget target = null;
    TransportMapping transport = null;
    String name = "Новый агент";

    public SnmpAgent() {

    }

    public String toString() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public SnmpAgent(String hostname, String port) throws ConfigurationException,
            java.io.IOException {
        if (port == null || port.length() == 0) {
            port = "161";
        }
        if (hostname == null || hostname.length() == 0) {
            throw new ConfigurationException(Errors.HOSTNAME_UNDEFINED);
        }

        targetAddress = GenericAddress.parse("udp:" + hostname + "/" + port);
        if (targetAddress == null) {
            throw new ConfigurationException(Errors.UNPARSABLE_NETWORK_ADDRESS);
        }

        transport = new DefaultUdpTransportMapping();
        snmp = new Snmp(transport);
        target = new CommunityTarget(targetAddress,
                new OctetString(Configuration.COMMUNITY_NAME));
        target.setAddress(targetAddress);
        target.setTimeout(Configuration.SNMP_TIMEOUT);
        target.setRetries(Configuration.RETRIES);
        target.setVersion(Configuration.SNMP_VERSION);
        transport.listen();
    }

    public VariableBinding[] walk(String OIDvalue) throws ProcessException,
            java.io.IOException {
        int size = 0;
        OID oid = new OID(OIDvalue);

        if (!oid.isValid()) {
            throw new ProcessException(Errors.INVALID_OID_VALUE);
        }

        System.out.println("Starting walk");
        PDU pdu = new PDU();
        pdu.add(new VariableBinding(oid));
        pdu.setType(PDU.GETNEXT);
        Vector<VariableBinding> vbTotColl = new Vector<VariableBinding>();
        boolean finish = false;

        while (!finish) {
            ResponseEvent responseEvent = snmp.send(pdu, target, transport);
            PDU response = responseEvent.getResponse();

            if (response != null) {
                for (int k = 0; k < response.size(); k++) {
                    VariableBinding vb = response.get(k);
                    if (vb.getOid().startsWith(oid)) {
                        vbTotColl.add(vb);
                        pdu.remove(0);
                        pdu.add(vb);
                        size++;
                    } else {
                        finish = true;
                        break;
                    }
                }
            }
        }

        VariableBinding[] a = new VariableBinding[size];
        return vbTotColl.toArray(a);
    }

    public void onResponse(ResponseEvent event) {
        System.out.println("Received " + event.toString());
    }

    public void setSnmpVersion(String version) {

    }
}
